﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Data;
using StoragePeriod.Web.Common;
using StoragePeriod.Common;

namespace StoragePeriod.Web.SysManage
{
    public partial class TeamManage : SecurityPage
    {
        #region 属性

        #endregion

        #region 继承
        public override string PrivCode()
        {
            return "e32e5bf8c18395b9";
        }
        #endregion

        #region 函数

        private void DataBing()
        {
            IQueryable<OperationTeam> RawType = OperationTeam.Where("ParentId<>@0", 0);
            this.RepDataList.DataSource = RawType;
            this.RepDataList.DataBind();
        }

        private void DropDataBing()
        {
            IQueryable<OperationTeam> Operation = OperationTeam.Where("ParentId=@0",0);
            this.Drop_Operation.DataSource = Operation;
            this.Drop_Operation.DataValueField = "id";
            this.Drop_Operation.DataTextField = "OTName";
            this.Drop_Operation.DataBind();
        }

        int i = 0;
        public string GetXH()
        {
            i += 1;
            return i.ToString();
        }

        public string GetTName(string Pid)
        {
            OperationTeam md = OperationTeam.Where("id=@0", int.Parse(Pid)).FirstOrDefault();
            return md == null ? "" : md.OTName;
        }

        #endregion

        #region 事件

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            DataBing();
            DropDataBing();
        }

        protected void RepDataList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                int RawTypeId = Convert.ToInt32(e.CommandArgument);
                OperationTeam md = OperationTeam.Where("id=@0", RawTypeId).First();
                string strTypeName = md.OTName;
                this.HidTypeID.Value = RawTypeId.ToString();

                UtilityUI.Load(this.upPanelMain, "LoadData('" + strTypeName + "','"+md.ParentId+"');ShowDlg(1)");
            }
            else
            {
                int RawTypeId = Convert.ToInt32(e.CommandArgument);
                OperationTeam.Delete("id=@0", RawTypeId);
                DataBing();
                UtilityUI.Load(this.upPanelMain, "alertstr('删除成功！')");
            }
        }

        protected void btPost_Click(object sender, EventArgs e)
        {
            if (this.HidIsEdit.Value == "0")
            {
                //添加
                OperationTeam md = new OperationTeam();
                md.ParentId = Convert.ToInt32(this.Drop_Operation.SelectedValue);
                md.OTName = this.txt_OperationName.Text.Trim();
                OperationTeam.Insert(md);
                DataBing();
            }
            else
            {
                //修改
                int RawTypeId = Convert.ToInt32(this.HidTypeID.Value);
                OperationTeam md = OperationTeam.Where("id=@0", RawTypeId).First();
                md.OTName = this.txt_OperationName.Text.Trim();
                md.ParentId = Convert.ToInt32(this.Drop_Operation.SelectedValue);
                OperationTeam.Update(md, "id=@0", RawTypeId);
                DataBing();
            }
        }

        #endregion
    }
}